package com.flower.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.flower.domain.ApiResult;
import com.flower.domain.entity.productSpecs.AttributeKey;
import com.flower.domain.entity.productSpecs.AttributeValue;
import com.flower.service.AttributeKeyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品sku控制层
 */
@RestController
@Slf4j
@RequestMapping("/attributeKey")
public class BackageAttributeKeyController {


    @Resource
    private AttributeKeyService attributeKeyService;

    /**
     * 根据分类id查找对应的sku
     * @param categoryId
     * @return
     */
    @GetMapping("/clist")
    public ApiResult<List<AttributeKey>> list(@RequestParam Long categoryId){
        return ApiResult.ok(attributeKeyService.cList(categoryId));
    }


    /**
     * 添加或者修改一个属性
     * @param attributeKey
     * @return
     */
    @PostMapping("/addAndup")
    public ApiResult<Boolean> attributeAddAndUp(@RequestBody AttributeKey attributeKey){
        if(attributeKey.getId()== null){
            attributeKeyService.save(attributeKey);
        }else {
            LambdaUpdateWrapper<AttributeKey> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(AttributeKey::getId,attributeKey.getId());
            wrapper.set(AttributeKey::getAttributeName,attributeKey.getAttributeName());
            attributeKeyService.update(wrapper);
        }
        return ApiResult.ok();
    }

    /**
     * 删除一个属性
     * @param attributeKeyId
     * @return
     */
    @GetMapping("/del/{attributeKeyId}")
    public ApiResult<Boolean> attributeDel(@PathVariable int attributeKeyId){
        attributeKeyService.removeById(attributeKeyId);
        return ApiResult.ok();
    }

}


